#ifndef TABLE_H
#define TABLE_H

#include <stddef.h>

//#include <raygui.h>
#include "rpak.h"

typedef struct TableState
{
	Vector2 anchor;
	int headerHeight, cellHeight;
	int cellNumWidth, cellNameWidth, cellTypeWidth, cellMiscWidth;
	Rectangle panelRec;
	Vector2 panelScroll;
	Rectangle panelView;
	int chkboxSize;
	unsigned int selectedIdx;
} TableState;

#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus

const char *fileTypeText[] =
{
	"#218#NONE",
	"#10#TEXT",
	"#12#IMAGE",
	"#11#AUDIO",
	"#30#FONT",
	"#162#MODEL"
};

TableState initTable(Rectangle *rec)
{
	TableState state = { 0 };

	state.anchor = (Vector2) { rec->x, rec->y };
	state.headerHeight = 28;
	state.cellHeight = 26;
	state.cellNumWidth = 60;
	state.cellTypeWidth = 160;
	state.cellMiscWidth = 100;
	state.cellNameWidth = rec->width - state.cellNumWidth - state.cellTypeWidth - state.cellMiscWidth * 2;

	state.panelRec = (Rectangle) { rec->x, rec->y + state.headerHeight, rec->width, rec->height - state.headerHeight };
	state.chkboxSize = 16;

	state.selectedIdx = 0;

	return state;
}

void drawTable(TableState *state, rpakFileInfo *files, unsigned int count)
{
	// header
	GuiStatusBar((Rectangle) { state->anchor.x, state->anchor.y, state->cellNumWidth + 1, state->headerHeight }, "No.");
	GuiStatusBar((Rectangle) { state->anchor.x + state->cellNumWidth, state->anchor.y, state->cellNameWidth + 1, state->headerHeight }, "File Name");
	GuiStatusBar((Rectangle) { state->anchor.x + state->cellNumWidth + state->cellNameWidth, state->anchor.y, state->cellTypeWidth + 1, state->headerHeight }, "File Type");
	GuiSetStyle(STATUSBAR, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
	GuiStatusBar((Rectangle) { state->panelRec.width - state->cellMiscWidth * 2, state->anchor.y, state->cellMiscWidth + 1, state->headerHeight }, "#179#Compressed");
	GuiStatusBar((Rectangle) { state->panelRec.width - state->cellMiscWidth, state->anchor.y, state->cellMiscWidth, state->headerHeight }, "#137#Encrypted");
	GuiSetStyle(STATUSBAR, TEXT_ALIGNMENT, TEXT_ALIGN_LEFT);

	// rows
	//----------------------------------------------------------------------------------------
	if (count > 0)
	{
		Rectangle rec = (Rectangle) { 0, 0, 0, state->cellHeight * count };
		bool showScrollbar = rec.height > state->panelRec.height;
		rec.width = state->panelRec.width - (showScrollbar ? 14 : 2); // 防止水平滚动条

		if (showScrollbar)
			GuiScrollPanel(state->panelRec, NULL, rec, &state->panelScroll, &state->panelView);

		Vector2 offset = (Vector2) { state->panelRec.x + state->panelScroll.x, state->panelRec.y + state->panelScroll.y };
		int chkboxLeft = (state->cellMiscWidth - state->chkboxSize) / 2;
		int chkboxY = offset.y + (state->cellHeight - state->chkboxSize) / 2.f;

		if (showScrollbar)
			BeginScissorMode(state->panelView.x, state->panelView.y, state->panelView.width, state->panelView.height);

		for (unsigned int i = 0; i < count; i++)
		{
			GuiSetStyle(BUTTON, BORDER_WIDTH, 0);
			if (state->selectedIdx == i)
			{
				GuiSetStyle(BUTTON, BASE_COLOR_NORMAL, 0xfff0b4ff);
				GuiSetStyle(BUTTON, BASE_COLOR_FOCUSED, 0xfff0b4ff);
			}
			else
				GuiSetStyle(BUTTON, BASE_COLOR_NORMAL, i % 2 == 0 ? 0xe1e1e1ff : 0xebebebff); // 225 : 235
			GuiSetStyle(BUTTON, BASE_COLOR_PRESSED, 0xfff0b4ff);
			if (GuiButton((Rectangle) { offset.x, offset.y, rec.width, state->cellHeight }, NULL) && state->selectedIdx != i)
			{
				state->selectedIdx = i;
			}
			GuiSetStyle(BUTTON, BORDER_WIDTH, 2);
			GuiSetStyle(BUTTON, BASE_COLOR_NORMAL, GuiGetStyle(DEFAULT, BASE_COLOR_NORMAL));
			if (state->selectedIdx == i)
				GuiSetStyle(BUTTON, BASE_COLOR_FOCUSED, GuiGetStyle(DEFAULT, BASE_COLOR_FOCUSED));
			GuiSetStyle(BUTTON, BASE_COLOR_PRESSED, GuiGetStyle(DEFAULT, BASE_COLOR_PRESSED));

			GuiLabel((Rectangle) { offset.x + 12, offset.y, state->cellNumWidth - (offset.x + 12), state->cellHeight }, TextFormat("%d", i + 1));
			GuiLabel((Rectangle) { offset.x + state->cellNumWidth, offset.y, state->cellNameWidth - offset.x, state->cellHeight }, (char *)files[i].fileName);
			GuiLabel((Rectangle) { offset.x + state->cellNumWidth + state->cellNameWidth, offset.y, state->cellTypeWidth - offset.x, state->cellHeight }, fileTypeText[files[i].fileType]);
			GuiCheckBox((Rectangle) { offset.x + state->panelRec.width - state->cellMiscWidth * 2 + chkboxLeft, chkboxY, state->chkboxSize, state->chkboxSize }, NULL, &files[i].compressed);
			GuiCheckBox((Rectangle) { offset.x + state->panelRec.width - state->cellMiscWidth + chkboxLeft, chkboxY, state->chkboxSize, state->chkboxSize }, NULL, &files[i].crypted);

			offset.y += state->cellHeight;
			chkboxY += state->cellHeight;
		}

		if (showScrollbar)
			EndScissorMode();
	}
	//----------------------------------------------------------------------------------------
}

#ifdef __cplusplus
}
#endif // __cplusplus

#endif // TABLE_H